import request from '@/utils/request'

// 组织架构相关API接口

/**
 * 获取组织架构树
 * @returns Promise<any>
 */
export const getOrgTree = () => {
  return request.get('/system/dept/tree')
}

/**
 * 获取部门详情
 * @param deptId 部门ID
 * @returns Promise<any>
 */
export const getDeptInfo = (deptId: string) => {
  return request.get(`/system/dept/info/${deptId}`)
}

/**
 * 获取部门成员列表
 * @param deptId 部门ID
 * @param params 查询参数
 * @returns Promise<any>
 */
export const getDeptMembers = (deptId: string, params: {
  page: number
  pageSize: number
  keyword?: string
}) => {
  return request.get(`/system/dept/members/${deptId}`, { params })
}

/**
 * 添加部门
 * @param data 部门数据
 * @returns Promise<any>
 */
export const addDept = (data: {
  parentId?: string
  name: string
  leaderId?: string
  description?: string
  status: number
}) => {
  return request.post('/system/dept/add', data)
}

/**
 * 更新部门
 * @param data 部门数据
 * @returns Promise<any>
 */
export const updateDept = (data: {
  id: string
  parentId?: string
  name: string
  leaderId?: string
  description?: string
  status: number
}) => {
  return request.put('/system/dept/update', data)
}

/**
 * 删除部门
 * @param deptId 部门ID
 * @returns Promise<any>
 */
export const deleteDept = (deptId: string) => {
  return request.delete(`/system/dept/delete/${deptId}`)
}

/**
 * 导入部门
 * @param file 部门数据文件
 * @returns Promise<any>
 */
export const importDept = (file: File) => {
  const formData = new FormData()
  formData.append('file', file)
  
  return request.post('/system/dept/import', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

/**
 * 导出部门
 * @param params 导出参数
 * @returns Promise<any>
 */
export const exportDept = (params?: {
  deptId?: string
}) => {
  return request.get('/system/dept/export', { 
    params,
    responseType: 'blob'
  })
}

/**
 * 添加部门成员
 * @param data 成员数据
 * @returns Promise<any>
 */
export const addDeptMember = (data: {
  deptId: string
  userId: string
  position?: string
}) => {
  return request.post('/system/dept/member/add', data)
}

/**
 * 移除部门成员
 * @param deptId 部门ID
 * @param userId 用户ID
 * @returns Promise<any>
 */
export const removeDeptMember = (deptId: string, userId: string) => {
  return request.delete(`/system/dept/member/remove/${deptId}/${userId}`)
}

/**
 * 批量转移部门成员
 * @param data 转移数据
 * @returns Promise<any>
 */
export const batchMoveDeptMembers = (data: {
  fromDeptId: string
  toDeptId: string
  userIds: string[]
}) => {
  return request.put('/system/dept/member/batch-move', data)
}

/**
 * 获取用户列表（用于选择部门负责人）
 * @param params 查询参数
 * @returns Promise<any>
 */
export const getUsers = (params?: {
  keyword?: string
  status?: number
  current?: number
  size?: number
}) => {
  return request.get('/users', { params })
}

/**
 * 获取部门可选上级部门列表
 * @param deptId 部门ID（排除当前部门及其子部门）
 * @returns Promise<any>
 */
export const getAvailableParentDepts = (deptId?: string) => {
  return request.get('/system/dept/available-parents', { 
    params: { deptId }
  })
}

/**
 * 移动部门位置
 * @param data 移动数据
 * @returns Promise<any>
 */
export const moveDept = (data: {
  deptId: string
  parentId: string
  targetOrder?: number
}) => {
  return request.put('/system/dept/move', data)
}

/**
 * 获取部门统计信息
 * @returns Promise<any>
 */
export const getOrgStatistics = () => {
  return request.get('/system/dept/statistics')
}